var teacherPage = {}
teacherPage.select=function (){
    var filterName = $('#filterName').val()
    $.post('teacher/select',{'filterName':filterName},function (view){
        $('#teacherContextFragment').replaceWith(view)
    })
}
teacherPage.clearQuery=function(){
    $('#filterName').val('')
    $.post('teacher/deleteAll',null,function (view){
        $('#teacherContextFragment').replaceWith(view)
    })
}
teacherPage.limitQuery=function(page){
    // alert($("#nav ul li[class='active']").val())

    // $("#nav ul li[class='active']").attr('class',null)

    var param = {}
    param.filterName=$('#filterName').val()
    param.page=page
    $.post('teacher/select',param,function (view){
        $('#teacherContextFragment').replaceWith(view)
    })
}
teacherPage.add=function (){
    $.post('teacher/addView',null,function (view){
        $('.modal-title').html('添加教师')
        $('.teacher-modal-body').html(view)
    })
    $('#teacherModal').modal('show')
}
teacherPage.commonSubmit=function (){
    var fileuploadValue = $('#importHideValue').val();
    // var upLoading = false;
    if (fileuploadValue=='file'){
        //ajax上传文件
        var file = $('#teacherFileUpload').val()
        if (file==''){
            alert('请选择上传的excel文件')
        }

        // if (upLoading){
        //     alert('文件正在上传，请稍候')
        //     return false;
        // }
        var files = $('#teacherFileUpload').prop('files');
        var data = new FormData();
        data.append('file', files[0]);
        $.ajax({
            url:"teacher/fileUpload",
            type:'POST',
            cache:false,
            data: data,
            processData:false,
            contentType:false,
            dataType:false,
            // beforeSend:function (){
            //     upLoading = true
            // },
            success:function (fileUploadVO){
                alert('本次成功的个数为:'+fileUploadVO.successCount+'\n'
                    +'失败的个数为:'+fileUploadVO.failCount+'\n'
                    +'具体情况如下:'+fileUploadVO.result.replaceAll('|','\n')
                )
                $('#teacherModal').modal('hide')
                teacherPage.clearQuery()
            }
        })
        return;
    }
    var value = $('#hiddenId').val();
    if (value==''){
        //表示是新建 功能  也可能是其他的
        teacherPage.submitAdd()
    }else {
        //修改
        var tname = $('#addTeacher').val()
        var id = $('#hiddenId').val()
        $.post("teacher/submitEdit",{"id":id,"tname":tname},function (result){
            if(result==0){
                alert('用户名存在')
            }else {
                alert('修改成功')
            }
            $('#teacherModal').modal('hide')
            teacherPage.clearQuery()
        })
    }

}
teacherPage.submitAdd=function (){
    var tname = $('#addTeacher').val()
    $.post('teacher/save',{'tname':tname},function (status){
        if (status==0){
            //这里好像有点毛病 后端设计不好  李四 和 李思 不同人 同拼音也不行
            alert('用户名重复或者存在相同拼音的名称,另外，请手动关闭，因为，我关闭不了')
        }else {
            alert('添加成功')
            $('#teacherModal').modal('hide')//这里就不作处理了  隐藏不了 这个模态框
            teacherPage.clearQuery();
        }
    })
}
teacherPage.deleteAll=function (){
    var param = $('#teacherContextFragment table tbody :checked')
    if (param.length==0){
        alert('请选择想要删除的用户')
        return;
    }
    if(!confirm('是否确认删除勾选内容')){
        return;
    }
    var value = '';
    param.each(function (i,element){
        value = value+element.value+','
    })
    $.post('teacher/deleteAlls',{'ids':value},function (){
        alert('删除成功')
        teacherPage.clearQuery()
    })
}
teacherPage.importAll=function (){
    $.post('teacher/importAll.html',null,function (view){
        $('#teacherModal .modal-title').html('导入用户')
        $('#teacherModal .teacher-modal-body').html(view)
    })
    $('#teacherModal').modal('show')
}

teacherPage.deleteOne=function (id){
    $.post('teacher/deleteOne',{"id":id},function (){
        alert('删除成功')
        teacherPage.clearQuery();
    })
}
teacherPage.edit=function (id){
    $.post('teacher/edit',{"id":id},function (view){
        $('.modal-title').html('编辑教师')
        $('.teacher-modal-body').html(view)
    })
    $('#teacherModal').modal('show')
}
teacherPage.selectAll=function (){
    var param = $('#teacherContextFragment table thead :checkbox').prop('checked')
    $('#teacherContextFragment table tbody :checkbox').prop('checked',param)
}
teacherPage.selectFile=function (){
    var value = $('#teacherFileUpload').val()
    if (value.endsWith('.xlsx')||value.endsWith('.xsl')){
        $('#fileContent').html(value)
    }else {
        alert('请选择正确的excel文件')
    }
}
